package com.wondersgroup.insurance.dnuilearn.views.pathmeasure;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/5/24.
 *  详见例子waveview
 */

public class WaterLoadView extends View {

    private static final int INT_WAVE_LENGTH = 1000;
    final int halfWaveLength = INT_WAVE_LENGTH/2;
    Paint mPaint;
    Path mPath =null;


    float ratio=0;

    private int mDeltaX;

    private Bitmap mBitMap;

    private float[] pos= new float[2];
    private float[] tan= new float[2];
    Matrix matrix = new Matrix();

    public WaterLoadView(Context context) {
        this(context, null);
    }

    public WaterLoadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaterLoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        setLayerType(LAYER_TYPE_SOFTWARE,null);
        init();
        handler.sendEmptyMessageDelayed(100,3000);
    }

    void init(){
        mPaint= new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(6);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPath = new Path();


        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize =  2;
        mBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.timg,options);
    }
   @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);



        mPath.reset();

        mPath.moveTo(-INT_WAVE_LENGTH + mDeltaX,getHeight()* (1-ratio));

//        mPath.moveTo(-INT_WAVE_LENGTH + mDeltaX,800);

        for(int i = -INT_WAVE_LENGTH ; i < getWidth() + INT_WAVE_LENGTH;
            i += INT_WAVE_LENGTH){
            mPath.rQuadTo(halfWaveLength/2,60,halfWaveLength,0);
            mPath.rQuadTo(halfWaveLength/2,-60,halfWaveLength,0);
        }


//        mPath.lineTo(getWidth(),getHeight()* (1-ratio));
        mPath.lineTo(getWidth(),getHeight());
        mPath.lineTo(0,getHeight());
        mPath.close();
        canvas.drawPath(mPath,mPaint);


        PathMeasure pathMeasure = new PathMeasure(mPath ,false);
        float length= pathMeasure.getLength();

       matrix.reset();
        /* 方案一*/
        boolean posTan = pathMeasure.getPosTan(length*ratio,pos,tan);
        if (posTan){
//            if (pos !=null)
//            Log.e("","pos[0] = " + pos[0] + "pos[1] = " +pos[1]);
//            if (tan != null)
//            Log.e("","tan[0] = " + tan[0] + "tan[1] = " +tan[1]);
            double degree = Math.toDegrees(Math.atan2(tan[1],tan[0]));

            matrix.postTranslate(pos[0]-mBitMap.getWidth()/2,pos[1]-mBitMap.getHeight());
            matrix.preRotate((float) degree,mBitMap.getWidth()/2, mBitMap.getHeight() / 2);


            canvas.drawBitmap(mBitMap,matrix,mPaint);
        }

//        Log.e("","pathMeasure.getLength() = "+length);




        /* 方案二*/
//       boolean isOk  = pathMeasure.getMatrix(length*ratio,matrix,PathMeasure.POSITION_MATRIX_FLAG|PathMeasure.TANGENT_MATRIX_FLAG);
//       if (isOk){//  ****************  preTranslate 同之前的矩阵运算*************************
//           matrix.preTranslate(-mBitMap.getWidth()/2,-mBitMap.getHeight());
//           canvas.drawBitmap(mBitMap,matrix,mPaint);
//       }

    }



    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            startAnimation();
        }
    };
    public void startAnimation(){
        ValueAnimator valueAnimator = null;
        if (ratio == 0){
            valueAnimator = ValueAnimator.ofFloat(0,1);
        }else {
            valueAnimator = ValueAnimator.ofFloat(1,0);

        }

        valueAnimator.setDuration(8000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ratio  = (Float) animation.getAnimatedValue();
//                mDeltaX = (int) (INT_WAVE_LENGTH*ratio);
                invalidate();
            }


        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                handler.sendEmptyMessageDelayed(100,2000);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.start();
    }


}
